New Operator
Used to create new instances of objects at runtime.
Syntax
The New operator has several different syntaxes:
When instantiating new instances of controls:
object reference = New ObjectName
Part | Type | Description |
object reference | Any object type | Required; variable of type object name. |
object name | Object name | Required. Any name of an object in the current window. |
When instantiating new instances all other types of objects:
object reference = New ObjectClass | ControlArrayName [(param1, param2,...)]
Part | Type | Description |
object reference | Any object type | Variable of type object class. |
object class | Object name | Name of any object class other than the control classes. |
ControlArrayName | Object name | The name of any control in the current window. |
param1...n | Any | Optional parameters passed to the object or control array. |
When creating and instantiating instances of all other types of objects:
Dim ObjectReference As New ObjectClass| ControlArrayName
Part | Type | Description |
ObjectReference | Any object type | Required; variable of type object class. |
ObjectClass | Object name | Required. Name of any object class other than the control classes. |
ControlArrayName | Object name | The name of any control in the current window. |
param1...n | Any | Optional parameters passed to the object or control array. |
[param1, param2,)]
Notes
The Constructor for a class is called whenever you create a new instance of it with the New statement.
The New operator can appear as a modifier on a Dim statement that creates a new object. This usage takes the place of two lines of code: The Dim statement that creates the object and a separate New statement that instantiates the object.
Creating new instances of controls or menu items
The New operator can only create a new instance of a control that already exists in the window or a menu item that already exists on a menu. The object name acts as a template for creating the new instance of the control or menu item. The New operator works if the control or menu item is in a control array. The object reference variable must be defined as type object name. For an example, see the section "Creating New Instances of Controls On The Fly" on page 271 of the User's Guide.
Creating new instances of all other types of objects
The New operator can be used to create a new instance of any type of object (for controls see the notes above). The object reference must be defined as being of type object class.
If the class's Visible property is set to True, the new instance will appear visibly in the running application.
Examples
This example creates a new instance of a pushbutton called "Pushbutton1" then sets its caption to "OK". PushButton1 has its Index property set to zero, making it the first element in a control array.
This example creates and then displays a new instance of a Window called "AboutBox".
This example creates a new instance of a MenuItem called "WindowItem1".
This example creates a new instance of a MessageDialog box.
It is equivalent to the following two lines:
See Also
Nil keyword; NewPicture functions.